import * as util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {    
    windowList:[],
    index:0,
    classList: [],
    periodList: [],
    window:{ name: '全部' },
    theClass: { name: '全部' },
    period: { name: '全部' },
    pdisable: true,
    canRequest: false,
    start: util.formatTime(new Date()),
    end: util.formatTime(new Date()),
    tableData: []
  },
  check: function(){
    let start = this.data.start
    let end = this.data.end
    this.setData({
      canRequest: !(start <= end)
    })
  },
  bindDateChangeStart: function (e) {
    this.setData({
      start: e.detail.value
    })
    this.check()
  },
  bindDateChangeEnd: function (e) {
    this.setData({
      end: e.detail.value
    })
    this.check()
  },
  getBase: function(){
    let _self = this
    getApp().baseInfo('client/company/' + getApp().globalData.companyId + '/window/list',function(res){
      console.log(res.data.data)
      res.data.data.unshift({name: '全部'})
      _self.setData({
        windowList: res.data.data,
        window: res.data.data[0],
        wdisable: false
      })
      getApp().baseInfo('client/food/category/list', function (res) {
        console.log(res.data.data)
        res.data.data.unshift({ name: '全部' })
        _self.setData({
          classList: res.data.data,
          theClass: res.data.data[0]
        })
        getApp().baseInfo('client/company/' + getApp().globalData.companyId + 'meal/list', function (res) {
          console.log(res.data.data)
          res.data.data.unshift({ name: '全部' })
          _self.setData({
            periodList: res.data.data,
            period: res.data.data[0],
            pdisable: false
          })  
          _self.getData()        
        }, function(){
          _self.getData()
        })
      })
    })
  },
  classChange: function(e){
    this.setData({
      theClass: this.data.classList[e.detail.value]
    })    
  },
  windowChange: function (e) {
    this.setData({
      window: this.data.windowList[e.detail.value]
    })
  },
  periodChange: function (e) {
    console.log(e)
    this.setData({
      period: this.data.periodList[e.detail.value]
    })
  },
  getData: function(){
    let app = getApp()
    let _self = this
    let postData = { 
      'companyId': app.globalData.companyId, 
      'windowId': _self.data.window.id, 
      'categoryId': _self.data.theClass.id, 
      'mealPeriod': _self.data.period.code, 
      'dateRange': [_self.data.start, _self.data.end] 
    }
    app.requestByData('food/sale/detail', postData, function(res){
      // console.log(res.data)      
      wx.hideLoading()
      if(res.data.status == 200){
        _self.setData({
          tableData: res.data.data
        })
        if(res.data.data.length == 0){
          wx.showToast({
            title: '暂无数据',
            icon: 'none'
          })
        }
      }else{
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showLoading({
      title: '加载中',
    })
    this.getBase()
  },

  /**
   * 顶部样式
   */
  onShareAppMessage: function () {
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({ title: '单品销量统计' });
  }
})